package com.ljd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljd.entity.Order;
import com.ljd.service.OrderService;
import com.ljd.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author ljd
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId,HttpServletRequest request){
        String orderId = orderService.createOrder(courseId,request);
        return R.ok().data("orderId",orderId);
    }
    @GetMapping("getOrder/{orderNo}")
    public R getOrder(@PathVariable String orderNo){
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderNo,orderNo);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("order",order);
    }
    @GetMapping("getOrder/{courseId}/{memberId}")
    public Boolean getOrder(@PathVariable String courseId,@PathVariable String memberId){
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getCourseId,courseId)
                .eq(Order::getMemberId,memberId)
                .eq(Order::getStatus,1);
        int count = orderService.count(wrapper);
        return count>0;
    }
}

